package com.gql.streamAPI;

import com.gql.methodReferences.Employee;
import com.gql.methodReferences.EmployeeData;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * description: 测试创建Stream
 * @author: gql
 * @date: 2022/04
 */
public class Stream01Test {

    /**
     * 方式1:通过集合
     */
    @Test
    public void listTest() {
        Supplier<List<Employee>> supplier = EmployeeData::getEmployees;
        List<Employee> employee = supplier.get();
        //**************************************************
        // 顺序流
        Stream<Employee> stream = employee.stream();
        // 并行流
        Stream<Employee> parallelStream = employee.parallelStream();
    }

    /**
     * 方式2:通过数组
     */
    @Test
    public void arraysTest(){
        int[] arr = {1,2,3,4,5,6,7};
        IntStream stream = Arrays.stream(arr);

         Employee employee1 = new Employee(1000,"Tom");
         Employee employee2 = new Employee(1001,"Jon");
         Employee[] arrEmploy = {employee1,employee2};
        Stream<Employee> stream1 = Arrays.stream(arrEmploy);
    }

    /**
     * 方式3:通过of
     */
    @Test
    public void ofTest(){
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    /**
     * 方式4:创建无限流(用来造数据)
     */
    @Test
    public void infiniteTest(){
        // 迭代:遍历前十个偶数
        Stream.iterate(0,t -> t+2).limit(10).forEach(System.out::println);
        // 生成:前十个随机数
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }
}
